import { wujiang, Wujiang } from "./u";
import { getEnemy, getEnemyByZhanfa, random, randomItem } from "./utils";
import { beidongZhanfa, zhihuiZhanfa, zhudongZhanfa, zhuijiZhanfa } from "./zhanfaKu";

// 伤害计算 num伤害率
export const shanghai = (zhanBao: string[], u1: Wujiang, u2: Wujiang, num: number, isMou: boolean = false) => {
  // 克制
  let k = 1;
  if ((u1.type === "骑" && u2.type === "弓") || (u1.type === "弓" && u2.type === "步") || (u1.type === "步" && u2.type === "骑")) {
    k = 0.8;
  }
  let randomNum = 0.8 + Math.random() * (1.2 - 0.8);
  let data = 0;
  let jiacheng = 1;
  if (u1.zhuangtai.shanghaijiangdi.jiacheng && u1.zhuangtai.shanghaijiangdi.huihe) {
    jiacheng = u1.zhuangtai.shanghaijiangdi.jiacheng;
  }
  if (isMou) {
    data = 4.8 * Math.sqrt(u1.hp + 100) * ((200 + u1.mou) / (200 + u2.mou)) * k * num * randomNum * jiacheng;
  } else {
    data = 4.8 * Math.sqrt(u1.hp + 100) * ((200 + u1.atk) / (200 + u2.def)) * k * num * randomNum * jiacheng;
  }
  if (u2.zhuangtai.guibi.num > 0 && u2.zhuangtai.guibi.huihe && random(u2.zhuangtai.guibi.jilv)) {
    zhanBao.push(`<span class="${u2.lr}">【${u2.name}】</span>受到战法【${u2.zhuangtai.guibi.laiyuanZhanfa}】影响，规避此次攻击`);
    data = 0;
    u2.zhuangtai.guibi.num--;
  }
  u1.shanghai += Number(data.toFixed(0));
  return Number(data.toFixed(0));
};

// 恢复率计算
const huifulv = (u: Wujiang, x: number, h: number, z: number) => {
  return x + h * (u.mou - z);
};
// 治疗量计算  x基础恢复率  h恢复增长率  z恢复额外需要加的谋略
export const zhiLiao = (zhanBao: string[], u: Wujiang, x: number, h: number, z: number) => {
  return ((300 * u.hp) / (3500 + u.hp)) * huifulv(u, x, h, z) * (1 + u.zhuangtai.jijiu.jiacheng);
};
// 是否急救
export const isJijiu = (zhanBao: string[], u: Wujiang) => {
  if (u.hp <= 0) return;
  if (u.zhuangtai.jijiu.num > 0 && random(u.zhuangtai.jijiu.jilv)) {
    let ji = Number(u.zhuangtai.jijiu.num.toFixed(0));
    if (u.zhuangtai.weikun.huihe) {
      ji = 0;
      zhanBao.push(`<span class="${u.lr}">【${u.name}】</span>受到战法【${u.zhuangtai.weikun.laiyuanZhanfa}】影响,无法恢复兵力`);
    }
    u.hp += ji;
    u.zhuangtai.jijiu.laiyuan.zhiliao += ji;
    zhanBao.push(`<span class="${u.lr}">【${u.name}】</span>受到战法【${u.zhuangtai.jijiu.laiyuanZhanfa}】影响,恢复${ji}兵力(${u.hp})`);
  }
};
export const lijiZhiliao = (zhanBao: string[], u1: Wujiang, u2: Wujiang, x: number, h: number, z: number) => {
  let ji = Number(zhiLiao(zhanBao, u1, x, h, z).toFixed(0));
  if (u2.zhuangtai.weikun.huihe) {
    ji = 0;
    zhanBao.push(`<span class="${u2.lr}">【${u2.name}】</span>受到战法【${u2.zhuangtai.weikun.laiyuanZhanfa}】影响,无法恢复兵力`);
  }
  u1.zhiliao += ji;
  return ji;
};
export const run = (a: any, b: any) => {
  // let l = [wujiang("孙权-吴-弓", 10000, 40, [], "l"), wujiang("刘备-蜀-步", 10000, 40, [], "l"), wujiang("曹操-魏-骑", 10000, 40, [], "l")];
  // let r = [wujiang("赵云-蜀-步-sp", 10000, 40, [], "r"), wujiang("关羽-蜀-骑", 10000, 40, [], "r"), wujiang("马超-群-骑", 10000, 40, [], "r")];

  let l: any = [];
  let r: any = [];
  // 初始化
  a.forEach((item: any) => {
    l.push(wujiang(item[0], item[1], item[2], item[3], item[4]));
  });
  b.forEach((item: any) => {
    r.push(wujiang(item[0], item[1], item[2], item[3], item[4]));
  });

  let zhanBao: string[] = [];
  let zhanwei = [...l, ...r];
  let LR = [...l, ...r];
  // 排序 速度
  LR.sort((a: Wujiang, b: Wujiang) => {
    return b.v - a.v;
  });

  let tuichuWujiangList: Wujiang[] = [];
  // 武将退出
  const isTuichu = (u: Wujiang) => {
    if (u.hp === 0) {
      zhanBao.push(`<span class="${u.lr}">【${u.name}】</span>无法再战`);
      LR = LR.filter((item) => item !== u);
      zhanwei = zhanwei.filter((item) => item !== u);
      tuichuWujiangList.push(u);
    }
  };
  const isOut = (zhanBao: string[]) => {
    if (l[0].hp === 0) {
      zhanBao.push(`<span class="${l[0].lr}">【${l[0].name}】</span>大营被击溃`);
      zhanBao.push(`游戏结束`);
      return true;
    }
    if (r[r.length - 1].hp === 0) {
      zhanBao.push(`<span class="${r[0].lr}">【${r[0].name}】</span>大营被击溃`);
      zhanBao.push(`游戏结束`);
      return true;
    }
    return false;
  };
  // 普通攻击
  const gongji = (u1: Wujiang, u2: Wujiang) => {
    zhanBao.push(`<span class="${u1.lr}">【${u1.name}】</span>对<span class="${u2.lr}">【${u2.name}】</span>发动普通攻击`);
    let sh = shanghai(zhanBao, u1, u2, 1);
    u2.hp -= sh;
    if (u2.hp <= 0) {
      u2.hp = 0;
    }
    zhanBao.push(`<span class="${u2.lr}">【${u2.name}】</span>损失${sh}兵力(${u2.hp})`);
    isTuichu(u2);
    isJijiu(zhanBao, u2);
    return { isOut: isOut(zhanBao), u2 };
  };

  // 阵容/士气
  LR.forEach((item: Wujiang) => {
    zhanBao.push(`<span class="${item.lr}">【${item.name}】</span> `);
  });
  // 设施
  // 宝物
  // 被动战法
  zhanBao.push(`被动战法释放`);
  LR.forEach((item: Wujiang) => {
    item.ji.forEach((strJi: any) => {
      let j: any = beidongZhanfa;
      if (j[strJi]) {
        j[strJi](zhanBao, item);
      }
    });
  });
  // 指挥战法
  zhanBao.push(`指挥战法释放`);
  LR.forEach((item: Wujiang) => {
    item.ji.forEach((strJi: any) => {
      let j: any = zhihuiZhanfa;
      if (j[strJi]) {
        j[strJi](zhanBao, item, zhanwei, l, r);
      }
    });
  });

  // 开始
  for (let i = 1; i < 9; i++) {
    zhanBao.push(`===========第${i}回合===========`);
    // 速度状态
    let arr1: Wujiang[] = [];
    let arr2: Wujiang[] = [];
    LR.forEach((item: Wujiang) => {
      if (item.zhuangtai.xianshou.huihe) {
        arr1.push(item);
      } else {
        arr2.push(item);
      }
    });
    let arr: Wujiang[] = [...arr1, ...arr2];
    for (let w = 0; w < arr.length; w++) {
      let item = arr[w];
      if (item.hp <= 0) continue;
      if (item.zhuangtai.celueshanghai.shoushang.includes(i)) {
        item.hp -= item.zhuangtai.celueshanghai.num;
        zhanBao.push(`<span class="${item.lr}">【${item.name}】</span>受到【${item.zhuangtai.celueshanghai.laiyuanZhanfa}】影响,受到(${item.zhuangtai.celueshanghai.num})伤害 (${item.hp})`);
        isTuichu(item);
      }

      if (item.zhuangtai.hunluan.huihe) {
        zhanBao.push(`<span class="${item.lr}">【${item.name}】</span>受到【${item.zhuangtai.hunluan.laiyuanZhanfa}】,处于混乱状态，无法行动`);
        let zhuangtaiList: any = ["hunluan", "youyu", "xianshou", "qiezhan", "baozou", "shanghaijiangdi", "lianji", "jijiu", "guibi", "fanji"];
        zhuangtaiList.forEach((zt: any) => {
          let t = item.zhuangtai as any;
          if (t[zt].huihe) {
            t[zt].huihe--;
          }
        });
        item.zhanfa = {};
        continue;
      }

      if (item.zhuangtai.youyu.huihe) {
        zhanBao.push(`<span class="${item.lr}">【${item.name}】</span>受到【${item.zhuangtai.youyu.laiyuanZhanfa}】,处于犹豫状态,无法发动战法`);
      } else {
        for (let k = 0; k < item.ji.length; k++) {
          let j: any = zhudongZhanfa;
          if (j[item.ji[k]]) {
            const isOut = j[item.ji[k]](zhanBao, item, zhanwei, l, r, tuichuWujiangList, LR);
            if (isOut) {
              return;
            }
          }
        }
      }
      if (item.zhuangtai.qiezhan.huihe && random(item.zhuangtai.qiezhan.jilv)) {
        zhanBao.push(`<span class="${item.lr}">【${item.name}】</span>受到【${item.zhuangtai.qiezhan.laiyuanZhanfa}】影响,处于怯战状态,无法普通攻击`);
      } else {
        if (getEnemyByZhanfa(zhanBao, item, zhanwei, item.distance).length > 0) {
          const { isOut, u2 } = gongji(item, randomItem(getEnemyByZhanfa(zhanBao, item, zhanwei, item.distance)));
          if (isOut) {
            return;
          }
          let j: any = zhuijiZhanfa;
          for (let k = 0; k < item.ji.length; k++) {
            if (j[item.ji[k]]) {
              const isOut2 = j[item.ji[k]](zhanBao, item, u2, zhanwei, tuichuWujiangList, LR);
              if (isOut2) {
                return;
              }
            }
          }
          if (item.zhuangtai.lianji.huihe && random(item.zhuangtai.lianji.jilv)) {
            const { isOut, u2 } = gongji(item, randomItem(getEnemyByZhanfa(zhanBao, item, zhanwei, item.distance)));
            if (isOut) {
              return;
            }
            let j: any = zhuijiZhanfa;
            for (let k = 0; k < item.ji.length; k++) {
              if (j[item.ji[k]]) {
                const isOut2 = j[item.ji[k]](zhanBao, item, u2, zhanwei, tuichuWujiangList, LR);
                if (isOut2) {
                  return;
                }
              }
            }
          }
        } else {
          zhanBao.push(`<span class="${item.lr}">【${item.name}】</span>攻击范围内没有敌军`);
        }
      }
      //   状态
      let zhuangtaiList: any = ["youyu", "xianshou", "qiezhan", "baozou", "shanghaijiangdi", "lianji", "jijiu", "guibi", "fanji"];
      zhuangtaiList.forEach((zt: any) => {
        let t = item.zhuangtai as any;
        if (t[zt].huihe) {
          t[zt].huihe--;
        }
      });
    }
  }

  zhanBao.push(`===========战斗结束===========`);

  return {
    zhanBao: zhanBao,
    wujiangList: [...zhanwei, ...tuichuWujiangList].map((item: any) => {
      delete item.zhuangtai;
      return item;
    }),
  };
};
